
<template>
  <div>
    <a-modal :visible="visible" centered :title="title" width="600px" @cancel="close" :destroyOnClose="true" @ok="handleOk">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item ref="name" label="角色名称" prop="name">
          <a-input
            v-model.trim="form.name"
            @blur="
              () => {
                $refs.name.onFieldBlur()
              }
            "
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>
<script>
import { editRole } from '@/api/pos/shareCode.js'
export default {
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      visible: false,
      title: '',
      form: {
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min:1,max: 15, message: '角色名不可超过15字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleOk() {
      const that = this
      console.log('=====', this.form.name)
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          let obj
          if (that.record == 'add') {
            // 新增
            obj = {
              name: that.form.name,
              operator: {
                operatorId: that.$store.getters.userInfo.workNo,
                operatorName: that.$store.getters.userInfo.realname
              }
            }
          } else {
            obj = {
              id: that.record.id,
              name: that.form.name,
              operator: {
                operatorId: that.$store.getters.userInfo.workNo,
                operatorName: that.$store.getters.userInfo.realname
              }
            }
          }
          editRole(obj).then(res => {
            if (res.code == 200) {
              that.visible = false
              that.form = {name:''}
              that.$emit('restOk')
            } else {
              that.$message.error(res.msg || res.message)
            }
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.form = {name:''}
      this.visible = false
    },
    show(record) {
      console.log(record, 'record')
      this.title = record == 'add' ? '新增' : '编辑'
      this.record = record
      if (record && record != 'add') {
        this.form.name = record.name
      }
      this.visible = true
    }
  }
}
</script>
      
      <style>
</style>